/// <summary>
// CodeFx - A .NET framework
// Laurent Morisseau
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// Laurent Morisseau
/// </summary>
/// <author><a href="mailto:laurent.morisseau@gmail.com">Laurent Morisseau</a></author>

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Security.Permissions;
using System.Reflection;

namespace CodeFx.Web
{
    [AttributeUsage(AttributeTargets.Class)]
    public sealed class PersistanceAttribute : Attribute 
	{
        private readonly string Key;
        
        private readonly EPersistanceMode Mode;

        private readonly EPersistanceLevel Level;

        private readonly EPersistanceScenario Scenarii;

		#region Data object
		public struct Data
		{
            public readonly EPersistanceScenario Scenarii;
            
            public readonly EPersistanceMode Mode;

			public readonly string Key;

			public readonly EPersistanceLevel Level;

			public Data( EPersistanceMode mode, string Key, EPersistanceLevel level )
			{
                this.Mode = mode;
                if (Key != null)
                    this.Key = Key;
                else
                    this.Key = string.Empty;
				this.Level = level;
                this.Scenarii = EPersistanceScenario.None;
			}
			public Data( PersistanceAttribute att )
			{
                if (att != null)
                {
                    this.Mode = att.Mode;
                    this.Key = att.Key;
                    this.Level = att.Level;
                    this.Scenarii = att.Scenarii;
                }
                else
                {
                    this.Mode = EPersistanceMode.None ;
                    this.Key = string.Empty ;
                    this.Level = EPersistanceLevel.None ;
                    this.Scenarii = EPersistanceScenario.None;
                }
			}
		} 
		#endregion

        public string key
        {
            get
            {
                return Key;
            }
        }

        public EPersistanceMode mode
        {
            get
            {
                return Mode;
            }
        }
        public EPersistanceLevel level
        {
            get
            {
                return Level;
            }
        }
        public EPersistanceScenario scenarii
        {
            get
            {
                return Scenarii;
            }
        }
		public PersistanceAttribute()
		{
			this.Mode = EPersistanceMode.None;
            this.Key = String.Empty;
            this.Level = EPersistanceLevel.None;
            this.Scenarii = EPersistanceScenario.None;
		}

        public PersistanceAttribute(string key)
        {
            this.Mode = EPersistanceMode.None ;
            if (key != null)
                this.Key = key;
            else
                this.Key = string.Empty;
            this.Level = EPersistanceLevel.None;
            this.Scenarii = EPersistanceScenario.None;
        }
        public PersistanceAttribute(EPersistanceMode mode)
        {
            this.Mode = mode;
            this.Key = String.Empty;
            this.Level = EPersistanceLevel.None;
            this.Scenarii = EPersistanceScenario.None;
        }

        public PersistanceAttribute(string key, EPersistanceMode mode)
        {
            this.Mode = mode;
            if (key != null)
                this.Key = key;
            else
                this.Key = string.Empty;
            this.Level = EPersistanceLevel.None;
            this.Scenarii = EPersistanceScenario.None;
        }

        public PersistanceAttribute(string key, EPersistanceMode mode, EPersistanceLevel level)
        {
            this.Mode = mode;
            if (key != null)
                this.Key = key;
            else
                this.Key = string.Empty;
            this.Level = level;
            this.Scenarii = EPersistanceScenario.None;
        }

        public PersistanceAttribute(string key, EPersistanceMode mode, EPersistanceLevel level, EPersistanceScenario scenarii)
        {
            this.Mode = mode;
            if (key != null)
                this.Key = key;
            else
                this.Key = string.Empty;
            this.Level = level;
            this.Scenarii = scenarii;
        }

		public static PersistanceAttribute Get( MemberInfo t )
        {
            if (t == null)
                return null;
			object[] objs = t.GetCustomAttributes( typeof( PersistanceAttribute ), true );
			if ( objs.Length == 0 )
				return null;
			else
				return (PersistanceAttribute)objs[ 0 ];
		}

        public static PersistanceAttribute.Data GetData(MemberInfo t)
		{
            if (t == null)
                return new PersistanceAttribute.Data();
			object[] objs = t.GetCustomAttributes( typeof( PersistanceAttribute ), true );
			if ( objs.Length == 0 )
				return new PersistanceAttribute.Data();
			else
				return new PersistanceAttribute.Data( ( PersistanceAttribute ) objs[ 0 ] );
		}

    }


}
